package by.dkusch.aircompany.webapp.page.regional.panel;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.OrderByBorder;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.FilterForm;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.PropertyModel;

import by.dkusch.aircompany.model.filter.FilterCountry;
import by.dkusch.aircompany.model.tables.Country;
import by.dkusch.aircompany.model.tables.Country_;
import by.dkusch.aircompany.services.CountryService;
import by.dkusch.aircompany.webapp.page.base.BaseLayout;
import by.dkusch.aircompany.webapp.page.base.common.AddNewItemPanel;
import by.dkusch.aircompany.webapp.page.base.common.CommonActionPanel;
import by.dkusch.aircompany.webapp.page.base.common.EntityDataProviderFilter;
import by.dkusch.aircompany.webapp.page.base.panels.MenuPanel;
import by.dkusch.aircompany.webapp.page.regional.CountryPage;

public class CountryListPanel extends Panel {

	private static final long serialVersionUID = 1L;
	@Inject
	private CountryService countryService;
	private EntityDataProviderFilter<Long, Country, FilterCountry> countryDataProvider;

	public CountryListPanel(String id, int itemsPerPage) {
		super(id);

		countryDataProvider = new EntityDataProviderFilter<Long, Country, FilterCountry>(countryService);
		countryDataProvider.setSort(Country_.code, SortOrder.ASCENDING);
		countryDataProvider.setFilterState(new FilterCountry());

		setOutputMarkupId(true);

		DataView<Country> dataView = new DataView<Country>("countryList", countryDataProvider, itemsPerPage) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(Item<Country> item) {
				final Country country = item.getModelObject();
				item.add(new CheckBox("arcFl").setEnabled(false));
				item.add(new Label("code"));
				item.add(new Label("name"));
				item.add(new Label("continent"));
				item.add(new Label("wikipediaLink"));
				item.add(new CommonActionPanel<Long, Country, CountryPage>("action-panel", countryService, country, CountryPage.class, CountryListPanel.this).setVisible(false));

			}
		};

		FilterForm<FilterCountry> filterForm = new FilterForm<FilterCountry>("filterForm", countryDataProvider);
		filterForm.add(new TextField<String>("filter-code", PropertyModel.<String> of(countryDataProvider, "filterState.code")));
		filterForm.add(new TextField<String>("filter-name", PropertyModel.<String> of(countryDataProvider, "filterState.name")));
		filterForm.add(new TextField<String>("filter-continent", PropertyModel.<String> of(countryDataProvider, "filterState.continent")));

		final WebMarkupContainer divTable = new WebMarkupContainer("div-table");
		final PagingNavigator pagingNavigator = new PagingNavigator("paging", dataView);
		divTable.setOutputMarkupId(true);
		pagingNavigator.setOutputMarkupId(true);

		filterForm.add(new AjaxButton("country-search", filterForm) {
			/**
				 * 
				 */
			private static final long serialVersionUID = 1L;

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so that it is hidden
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
				target.add(pagingNavigator);
			}

			@Override
			protected void onError(AjaxRequestTarget target, Form<?> form) {
				// repaint the feedback panel so errors are shown
				target.add(((MenuPanel) ((BaseLayout) getPage()).getMenuPanel()).getFeedback());
				target.add(divTable);
			}
		});

		divTable.add(dataView);
		divTable.add(new AddNewItemPanel<Long, Country, CountryPage>("new-panel", new Country(), CountryPage.class).setVisible(false));


		divTable.add(new OrderByBorder<SingularAttribute<Country, ?>>("sortByName", Country_.name, countryDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Country, ?>>("sortByCode", Country_.code, countryDataProvider));
		divTable.add(new OrderByBorder<SingularAttribute<Country, ?>>("sortByContinent", Country_.continent, countryDataProvider));

		filterForm.add(divTable);
		add(pagingNavigator);
		add(filterForm);

	}

}
